#ifndef functiont_H
#define functiont_H

#include <QThread>
#include <QtCore>
#include <QtNetwork>
#include "math.h"
#include<iostream>
#include<string>
#include<QDebug>
#include <thread>
#include<sstream>
#include <QList>
#include "ctime"
#include "cstdlib"
#include<iomanip>
#include "stdlib.h"
#include<vector>
#include<QMessageBox>
#include <QCoreApplication>
#include <QMainWindow>
#include "qfile.h"
#include "qtextstream.h"
#include <QDesktopServices>
#include <QMessageBox>
#include <QFileDialog>
#include "fstream"
#include "qprocess.h"

#define PI 3.1415926
using namespace std;

typedef struct
{
    bool isValid;//是否有反馈
    QString Name;
    QString MAC;

    QString IP;
    QString netmask;
    QString broadcast;

} NetWorkInfo;

//数组
typedef struct
{
    double DATA;
    bool Isvalid;//这个数据是否有效
} LREALArray1;

typedef struct
{
    bool DATA;
    bool Isvalid;//这个数据是否有效
} BOOLArray1;

typedef struct
{
    QString DATA;
    bool Isvalid;//这个数据是否有效
} QStringArray1;

struct LREALArray10
{
    double DATA[10];
    bool Isvalid;//这个数据是否有效
};

struct INTArray10
{
    int DATA[10];
    bool Isvalid;//这个数据是否有效
};

//获取字符串指令的结构体
typedef struct
{
    int TargetAxis;
    int TargetDirection;

    LREALArray10 TargetPosition;//目标位置
    bool IfBlocked;//返回的结果

}PackageStringCommand;

struct AxisInfoTotal
{
    INTArray10 Statusword;//#0x6041,UINT,16
    INTArray10 Errorcode;//#0x603F,UINT,16
    INTArray10 CurrentPosition;//#0x6064,DINT,32
    INTArray10 CurrentVelocity;//#0x606C,DINT,32
    INTArray10 CurrentTorque;//#0x6077,INT,16
    INTArray10 CurrentPositionFollowError;//#0x60F4,DINT,32


    INTArray10 Controlword;//#0x6040,UINT,16
    INTArray10 Modesofoperation;//#0x6060,SINT,8
    INTArray10 TargetPosition;//#0x607A,DINT,32
    INTArray10 TargetVelocity;//#0x60FF,DINT,32
    INTArray10 TargetTorque;//#0x6071,INT,16

};
struct QStringArray10
{
    QString DATA[10];
    bool Isvalid;//这个数据是否有效
};

class functiont:public QThread
{
    Q_OBJECT

public:
    functiont();
    ~functiont();

    //这样写就所有类的实例都是指向唯一对象了
    static functiont *instance(void);

    int job = 0;//0 chushi 1 kaiqi 2yunxing -1 guanbi
    int RobotAxisNum=4;
    int DoPowerONIndex=-1;
    double theRadius[4];
    //all joint info(现在这个是ETHERCAT外部轴的)
    AxisInfoTotal RobotHardwareInfo;

    double TargetPositionfinal[10];
    double CurrentPositionReal[10];
    double tempVector[4];
    double CurrentPositionRealRadius[10];
    double joint_calibrateBias[4]={-96519664.000,-95253024.000,-134213552.000,-92523792.000};
    double joint_direction[4]={1, 1, 1, 1};
    double joint_gearratio[4]={895.061,895.061,895.061,895.061};

    LREALArray10 StatusWord=LREALArray10();
    LREALArray10 ErrorCode=LREALArray10();
    QStringArray10 StatusWordString=QStringArray10();
    QStringArray10 ErrorCodeString=QStringArray10();

    //定时器定时次数
    int timerCount=0;
    QTimer *timer= new QTimer;//定时器

    int stopwatchLastTime;
    bool stopwatchifPaused;
    QTime *stopwatch = new QTime;//秒表

    void AxisPowerONALL();
    void AxisPowerOFFALL();
    void AxisClearErrorALL();
    void AxisPowerONSingle(int axis);
    void AxisPowerOFFSingle(int axis);

    double r2d(double Radius);


    string ChangeJointArrayToString(double* JointDATA,int RealNum,int Percision);
    void parseStringToDoubleArray(const QString& str, double result[4]);

    NetWorkInfo GetTargetIPInfo(int index);
    void timerStart();
    void timerInit(int InputTick);
    //QString RobotRunStringCommandInThread(QString Command);
    double ChangeEncoderToRealValue_Pos(double EncoderValue,int EncoderIndex);
    double ChangeRealToEncoderValue_Pos(double RealValue,int EncoderIndex);
    double d2r(double Degree);
    void MAINLOOP();
};

#endif // functiont_H
